package com.windward.vii.web.controller.filter;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import java.util.Arrays;
import java.util.List;

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class FilterConfig {

    @Value("${web.filter.login:/v1/api1/*;/v2/api1/*}")
    private String loginUrlPattern;

//    @Value("${web.filter.login:/v1/api0/*}")
//    private String loginUrlPattern;

    @Bean
    public FilterRegistrationBean anonTokenCheckFilter() {
        RequestFilter filter = createAnonRequestFilter();
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(filter);
        List<String> urlPatterns = Arrays.asList(loginUrlPattern.split(";"));
        registrationBean.setUrlPatterns(urlPatterns);
        return registrationBean;
    }

    @Bean
    public RequestFilter createAnonRequestFilter() {
        return new RequestFilter();
    }

}